package com.ruicar.afs.cloud.channel.mange.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.channel.mange.condition.RoleTypeCondition;
import com.ruicar.afs.cloud.channel.mange.entity.ChannelUserRole;
import com.ruicar.afs.cloud.channel.mange.entity.RoleType;
import com.ruicar.afs.cloud.channel.mange.enums.CarTypeEnum;
import com.ruicar.afs.cloud.channel.mange.service.RoleTypeService;
import com.ruicar.afs.cloud.common.core.security.service.AfsUser;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xudongdong
 * @since 2020-06-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("/roleType")
@Api(value = "roleType", description = "角色类别模块")
public class RoleTypeController {
    private final RoleTypeService roleTypeService;

    /**
     * 分页获取全部角色类别
     */
    @PostMapping(value = "/getRoleTypeData")
    @ApiOperation(value = "分页获取全部")
    public IResponse<List<RoleType>> getRoleTypeData(@RequestBody RoleTypeCondition condition) {
        Page page = new Page(condition.getPageNumber(), condition.getPageSize());
        IPage<List<RoleType>> list = roleTypeService.page(page, Wrappers.<RoleType>query());
        return IResponse.success(list);
    }

    /**
     * 根据Id获取角色类别
     */
    @PostMapping(value = "/getRoleTypeById")
    @ApiOperation(value = "根据Id获取角色类别")
    public IResponse getRoleTypeById() {
        AfsUser user = SecurityUtils.getUser();
        List<RoleType> list = roleTypeService.list(Wrappers.<RoleType>query().lambda().in(RoleType::getRoleId, user.getRoleIdList()));
        return new IResponse().setData(list.get(0));
    }

    /**
     * 保存角色类别
     */
    @PostMapping("/save")
    public IResponse<Boolean> save(@Valid @RequestBody RoleTypeCondition condition) {
        RoleType roleType = new RoleType();
        if (condition.getRoleTypes().length > 1) {
            roleType.setRoleType(CarTypeEnum.newCaroldCar.name());
        } else {
            roleType.setRoleType(condition.getRoleTypes()[0]);
        }
        if (condition.getFlag() == 0) {
            for (int i = 0; i < condition.getRoleIds().length; i++) {
                roleType.setRoleId(condition.getRoleIds()[i]);
                roleType.setRoleName(condition.getRoleNames()[i]);
                roleTypeService.save(roleType);
            }
        } else if (condition.getFlag() == 1) {
            for (int i = 0; i < condition.getRoleIds().length; i++) {
                roleType.setRoleId(condition.getRoleIds()[i]);
                roleType.setRoleName(condition.getRoleNames()[i]);
                roleTypeService.update(roleType, Wrappers.<RoleType>query().lambda().eq(RoleType::getRoleId, condition.getRoleIds()[i]));
            }
        }

        return new IResponse<Boolean>().setMsg("编辑成功").setData(true);
    }

    /**
     * 删除角色类别
     */
    @RequestMapping(value = "/deleteRoleType/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "通过id删除")
    @Transactional
    public IResponse<Boolean> delAllByIds(@PathVariable Long id) {

        //删除角色类别
        roleTypeService.remove(Wrappers.<RoleType>query().lambda().eq(RoleType::getRoleId, id));
        return new IResponse<Boolean>().setMsg("批量通过id删除数据成功");
    }

    @Transactional
    @PostMapping("/resetRole")
    @ApiOperation(value = "清除无效的角色信息")
    public IResponse<Boolean> resetRole(@RequestBody JSONObject json) {
        List<ChannelUserRole> roles =json.getJSONArray("roles").toJavaList(ChannelUserRole.class);
        // 声明 角色id集合
        List<Long> ids = new ArrayList<>();
        if(roles.size()>0){
            roles.forEach(en->{
                ids.add(en.getId());
            });
            roleTypeService.remove(Wrappers.<RoleType>query().lambda().notIn(RoleType::getRoleId,ids));
        }
        return new IResponse<Boolean>().setMsg("清除无效的角色成功");
    }

    @PostMapping("/getAllRole")
    @ApiOperation(value = "清除无效的角色信息")
    public IResponse<List<RoleType>> getAllRole() {
        return IResponse.success(roleTypeService.list());
    }


}
